package com.lipaluma.field.validator.impl.numeric;

import java.lang.reflect.Field;
import java.util.List;

import com.lipaluma.annotations.validations.ValidateInteger;
import com.lipaluma.context.Context;
import com.lipaluma.field.validator.ValidationError;
import com.lipaluma.field.validator.impl.generic.AbstractConstantValidator;
import com.lipaluma.xml.validator.XmlConstantValidator;
import com.lipaluma.xml.validator.XmlValidator;

/**
 * Validate a integer type field (Short, short, Integer, int, Long, long) to check if all value for the field is constant.<br/>
 * A value can be supplied or not. 
 * <ul>
 * <li>If the value is supplied, all values of the source must be equal to that value</li>
 * <li>If not, the first value encountered in the source will be the constant to respect for all others.</li>
 * </ul>
 * 
 * <p>
 * For Initialisation :
 * <ul>
 * 	<li>
 * 	<u><b>By annotations :</b></u> Use {@link ValidateInteger}, with the attribute <code>constant = true</code>. <br/>
 * To supply a value, put <code>constantSupplied = true</code> and the value supplied in <code>constantValue</code> attribute.<br/>
 * </li>
 * 	<li>
 * 	<u><b>By Xml :</b></u>
 * <ul>
 * <li>
 * See the example below to add a constant validator without supplied value : <br/>
 * <code>
 * &lt;ToMap field="..."   ...><br/>
 * &nbsp;&nbsp;&nbsp;<b>&lt;constant/></b><br/>
 * &lt;/ToMap>
 * </code>
 * <br/>
 * </li>
 * <li>
 * See this other example to add a constant validator with a supplied value : <br/>
 * <code>
 * &lt;ToMap field="..."   ...><br/>
 * &nbsp;&nbsp;&nbsp;<b>&lt;constant>12124&lt;/constant></b><br/>
 * &lt;/ToMap>
 * </code>
 * <br/>
 * </li>
 * </li>
 * </ul>
 * @author Mario
 *
 */
public class LConstantValidator extends AbstractConstantValidator<Long> {

	/**
	 * Default constructor with no value supplied
	 */
	public LConstantValidator() {}
	
	/**
	 * Specify a supplied value (short)
	 * @param constant the constant supplied
	 */
	public LConstantValidator(short constant) {
		super(new Long(constant));
	}

	/**
	 * Specify a supplied value (integer)
	 * @param constant the constant supplied
	 */
	public LConstantValidator(int constant) {
		super(new Long(constant));
	}
	/**
	 * Specify a supplied value (long)
	 * @param constant the constant supplied
	 */
	public LConstantValidator(long constant) {
		super(constant);
	}

	public boolean init(Context context, Field field) {
		ValidateInteger annotation = field.getAnnotation(ValidateInteger.class);
		if(annotation == null || !annotation.constant())
			return false;		
		if(annotation.constantSupplied()) {
			init(annotation.constantValue());
		}
		return true;
	}

	public boolean init(Context context, Field field, XmlValidator xmlValidator) {
		XmlConstantValidator validator = (XmlConstantValidator) xmlValidator;
		if(validator.getConstantValue() != null) {
			init(validator.getConstantValue(Long.class));
		}
		return true;
	}	

	@Override
	public boolean validate(Object value, List<ValidationError> errors) {
		return super.validate(new Long(value.toString()), errors);
	}

}
